"""
This file is used to fill tables having informmation from which sites questions are migrated to a particular site

superuser -->stackoverflo
programmer-->stackoverflow
meat-->stackoverflow
"""
import MySQLdb

database = "isec2014"
table = "to_migration"
user = "root"
password="123"
port=3306

file1="D:\Research\ISEC-2014\data\\QueryResults_from_so.csv"

def read_table_from_sites():
    f1 = open(file1)
    newline = list()
    for line in f1:
        newline.append(line)
        #print "line=", line
    return newline
    
"""def read_table_from_sites():    
    f1 = open(file1)
    for line in f1:
        newline = 0
        print "line=", line
    print "reading"    
"""
def  get_to_site():
    return "stackoverflow"

def parse_and_enter_sites():
     to_site = get_to_site()
     fromSites=read_table_from_sites()
     all_from_sites = list()
     for site in fromSites:
         site = site.split(" ")[1]
         site = site.split("/")[2]
         site=site.split(".")[0]
         all_from_sites.append(site)

     db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
     insert_cursor = db1.cursor()  
     
     postid = 0
     for fromSite in all_from_sites:
         postid = postid+1
         insert_str = "insert into "+ table+" values( "+(str)(postid)+",'"+fromSite+"','"+to_site+"')"
         insert_cursor.execute(insert_str)
     
     db1.commit()
     db1.close()  
         #print site
     print "processing"

def get_all_sites():
    db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
    get_cursor = db1.cursor()
    select_str1 = "select distinct initial from "+table
    select_str2 = "select distinct migrted from "+table
    
    get_cursor.execute(select_str1)
    
    
    
    get_cursor.execute(select_str2)
    
    db1.close()
    all_sites_dummy = ["stackoverflo","drupl","meta","tex"]
    return  
     
     

def create_matrix():
    #get all the 50 sites
    sites_50 = get_all_sites()
    #read from the database
    #create matrix
    #write to csv/excels sheet
    print "hello"    


print "reading"


"""=====Not a comment====="""
parse_and_enter_sites()

print "creating matrix"
create_matix()    
    
    